﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace PocketBook.Model
{
    public class DesignFolder : IDesignFolder
    {
        private readonly List<IDesignFolder> _subFolders = new List<IDesignFolder>();
        private readonly List<IDesign> _designs = new List<IDesign>();

        public DesignFolder(string displayName)
        {
            Contract.Requires(displayName != null);
            DisplayName = displayName;
        }

        public string DisplayName
        {
            get; private set;
        }

        public IEnumerable<IDesign> Designs
        {
            get { return _designs; }
        }

        public IEnumerable<IDesignFolder> SubFolders
        {
            get { return _subFolders; }
        }

        public void Add(IDesign design)
        {
            Contract.Requires(design != null);
            _designs.Add(design);
        }

        public DesignFolder GetOrCreateSubFolder(string folderName)
        {
            if (String.IsNullOrEmpty(folderName)) return this;

            var subFolder = (DesignFolder) _subFolders.FirstOrDefault(folder => folder.DisplayName == folderName);

            if (subFolder == null)
            {
                subFolder = new DesignFolder(folderName);
                _subFolders.Add(subFolder);
            }

            return subFolder;
        }

        public IEnumerator GetEnumerator()
        {
            return (_designs.Concat<object>(_subFolders)).GetEnumerator();
        }
    }
}